package ipad.asia.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import ipad.asia.config.AjaxObj;
import ipad.asia.entity.Enterprise;
import ipad.asia.entity.Order;
import ipad.asia.entity.Shop;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.service.order.OrderFeign;
import ipad.asia.service.shop.ShopFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SocketToAjaxController {
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EnterpriseFeign enterpriseFeign;
    @Autowired
    private ShopFeign shopFeign;

    //JSON化参数
    @RequestMapping(value = "ajax/userName")
    public String name(HttpServletResponse response,HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));//处理跨域
        response.setHeader("Access-Control-Allow-Credentials", "true");//表示是否允许发送Cookie
        String shopName = shopFeign.findById((Integer) request.getSession().getAttribute("shopId")).getName();
        return shopName;
    }



    @RequestMapping("ajax/ConsumerList")
    public List<String> ConsumerList(HttpServletResponse response,
            HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));//处理跨域
        response.setHeader("Access-Control-Allow-Credentials", "true");//表示是否允许发送Cookie
        System.out.println("测试："+request.getSession().getAttribute("user"));
        if (request.getSession().getAttribute("user") == null) {
            List<String> strings=new ArrayList<>();
            strings.add("登录失效，请重新登录！");
            return strings;
        } else {
            String user = (String) request.getSession().getAttribute("user");
            Enterprise enterprise = enterpriseFeign.getName(user);
            System.out.println(enterprise);
            //获取店铺订单
            List<Order> orders = orderFeign.findByShopId(enterprise.getShopId());
            //获取订单中的用户id(不可重复)
            List<Integer> consumerIds = new ArrayList<>();
            for (int i = 0; i < orders.size(); i++) {
                if (!consumerIds.contains(orders.get(i).getConsumerId())) {
                    consumerIds.add(orders.get(i).getConsumerId());
                } else continue;
            }
            System.out.println(orders);
            //获取订单中的用户名
            List<String> consumerList = new ArrayList<>();
            for (int i = 0; i < consumerIds.size(); i++) {
                consumerList.add(consumerFeign.getId(consumerIds.get(i)).getUser());
            }
            System.out.println(consumerList);
            return consumerList;
        }
    }

}
